﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using oms;

public class DebugInfo : MonoBehaviour {

    EventControl event_control = new EventControl();

    void Awake()
    {
        

        event_control.RegisterGlobal("haha", this.a);
        event_control.RegisterGlobal("haha", this.b);
        event_control.TriggerGlobal("haha");

    }

    void a(string msg, params object[] objs)
    {
        Debug.Log("a");
        event_control.RemoveGlobal("haha", this.b);
    }

    void b(string msg, params object[] objs)
    {
        Debug.Log("b");
    }

    void c(params object[] objs)
    {
        Debug.Log("c");
    }

    void OnEnable()
    {
        Application.logMessageReceived += HandleLog;
    }
    void OnDisable()
    {
        Application.logMessageReceived -= HandleLog;
    }
    private string m_logs;
    /// <summary>
    /// 收集日志
    /// </summary>
    /// <param name="logString">错误信息</param>
    /// <param name="stackTrace">跟踪堆栈</param>
    /// <param name="type">错误类型</param>
    void HandleLog(string logString, string stackTrace, LogType type)
    {
        m_logs += logString + "\n";
    }

    private Vector2 _scroll_pos;
    private void _ShowInfo(string info)
    {
        _scroll_pos = GUILayout.BeginScrollView(_scroll_pos);
        GUI.skin.label.normal.textColor = Color.red;
        GUILayout.Label(info);
        GUILayout.EndScrollView();
    }
    
    private void DumpLog()
    {
        _ShowInfo(m_logs);
    }

    private void DumpApplicationProperty()
    {
        string s = "Application的静态属性：\n\n";

        List<string> property_infos = new List<string>();
        foreach (System.Reflection.PropertyInfo info in typeof(Application).GetProperties())
        {
            property_infos.Add(info.Name + " = " + info.GetValue(null, null));
        }
        property_infos.Sort();

        s += String.Join("\n", property_infos.ToArray());

        _ShowInfo(s);
    }


    public bool m_show_log = false;
    public bool m_show_application = true;
    [ExecuteInEditMode]
    void OnGUI()
    {
        if(m_show_log)
        {
            DumpLog();
            return;
        }
        if(m_show_application)
        {
            DumpApplicationProperty();
            return;
        }
    }
}
